package com.hunttown.mes.impl;

import com.hunttown.mes.convert.AnalysisManageGroupConverts;
import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.rpc.api.AnalysisManageGroupRpcService;
import com.hunttown.mes.rpc.domain.AnalysisManageGroupDTO;
import com.hunttown.mes.rpc.domain.query.AnalysisManageGroupDTOQuery;
import com.hunttown.mes.service.AnalysisManageGroupService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.hunttown.mes.convert.AnalysisManageGroupConverts.convert;

/**
 * created by wangjunfu on 2018-11-26 15:05:22
 */
@Service
public class AnalysisManageGroupRpcServiceImpl implements AnalysisManageGroupRpcService {

    private final Logger logger = LoggerFactory.getLogger(AnalysisManageGroupRpcServiceImpl.class);

    private final AnalysisManageGroupService analysisManageGroupService;

    @Autowired
    public AnalysisManageGroupRpcServiceImpl(AnalysisManageGroupService analysisManageGroupService) {
        this.analysisManageGroupService = analysisManageGroupService;
    }

    @Override
    public AnalysisManageGroupDTO insert(AnalysisManageGroupDTO analysisManageGroupDTO) {
        try {
            return convert(analysisManageGroupService.insert(AnalysisManageGroupConverts.convert(analysisManageGroupDTO)));
        } catch (Exception e) {
            logger.error("AnalysisManageGroup create failed.", e);
            throw new RuntimeException("AnalysisManageGroup create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(AnalysisManageGroupDTO analysisManageGroupDTO) {
        try {
            return analysisManageGroupService.updateInfoById(AnalysisManageGroupConverts.convert(analysisManageGroupDTO));
        } catch (Exception e) {
            logger.error("AnalysisManageGroup update by id failed.", e);
            throw new RuntimeException("AnalysisManageGroup update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(AnalysisManageGroupDTOQuery analysisManageGroupDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (analysisManageGroupDTOQuery.getW_id() != null && analysisManageGroupDTOQuery.getW_id() > 0) {
                query.put("w_id", analysisManageGroupDTOQuery.getW_id());
            }

            if (analysisManageGroupDTOQuery.getIds() != null && !analysisManageGroupDTOQuery.getIds().equals("")) {
                query.put("ids", analysisManageGroupDTOQuery.getIds());
            }

            if (analysisManageGroupDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", analysisManageGroupDTOQuery.getStopFlag());
            }

            if (analysisManageGroupDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", analysisManageGroupDTOQuery.getDeleteFlag());
            }

            return analysisManageGroupService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("AnalysisManageGroup update by query failed.", e);
            throw new RuntimeException("AnalysisManageGroup update by query failed, Please order your code.", e);
        }
    }

    @Override
    public AnalysisManageGroupDTO selectById(Integer id) {
        try {
            return convert(analysisManageGroupService.selectById(id));
        } catch (Exception e) {
            logger.error("AnalysisManageGroup select by id failed.", e);
            throw new RuntimeException("AnalysisManageGroup select by id failed, Please order your code.", e);
        }
    }

    @Override
    public AnalysisManageGroupDTO selectObjByQuery(AnalysisManageGroupDTOQuery analysisManageGroupDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            return convert(analysisManageGroupService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("AnalysisManageGroup select obj by query failed.", e);
            throw new RuntimeException("AnalysisManageGroup select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<AnalysisManageGroupDTO> getListForPage(AnalysisManageGroupDTOQuery analysisManageGroupDTOQuery) {
        Query query = Query.page(analysisManageGroupDTOQuery.getPn(), analysisManageGroupDTOQuery.getPageSize());

        if (analysisManageGroupDTOQuery.getIds() != null && !analysisManageGroupDTOQuery.getIds().equals("")) {
            query.put("ids", analysisManageGroupDTOQuery.getIds());
        }

        if (analysisManageGroupDTOQuery.getLikeName() != null && !analysisManageGroupDTOQuery.getLikeName().equals("")) {
            query.put("likeName", analysisManageGroupDTOQuery.getLikeName());
        }

        if (analysisManageGroupDTOQuery.getOrderBy() != null && analysisManageGroupDTOQuery.getOrderBy().length() > 0) {
            query.put("orderBy", analysisManageGroupDTOQuery.getOrderBy());
        }

        List<AnalysisManageGroupDTO> list = convert(analysisManageGroupService.selectByQuery(query));
        Page<AnalysisManageGroupDTO> page = Page.from(analysisManageGroupDTOQuery.getPn(), analysisManageGroupDTOQuery.getPageSize(), list);
        page.setTotalItems(analysisManageGroupService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return analysisManageGroupService.deleteById(id);
        } catch (Exception e) {
            logger.error("AnalysisManageGroup delete failed.", e);
            throw new RuntimeException("AnalysisManageGroup delete failed, Please order your code.", e);
        }
    }
}